﻿
using System;

namespace WooEngine.Utility.Security.Base
{
	[System.Serializable]
	public class EncryptFloat
	{
		public static System.Random random = new System.Random();

		private byte SECRET_KEY;

		private byte[] _value;

		private byte[] _convertTempBytes;
		public float Value
		{
			set 
			{
				byte[] bytes = BitConverter.GetBytes(value);
				for(int i=0 ; i < bytes.Length ; i++)
				{
					bytes[i] = (byte)(bytes[i] ^ SECRET_KEY);
				}
				_value = bytes;
			}
			get 
			{ 
				if(_convertTempBytes == null || _convertTempBytes.Length != _value.Length)
				{
					_convertTempBytes = new byte[_value.Length];
				}

				for(int i=0 ; i < _value.Length ; i++)
				{
					_convertTempBytes[i] = (byte)(_value[i] ^ SECRET_KEY);
				}
				return BitConverter.ToSingle(_convertTempBytes, 0);
			}
		}

		public EncryptFloat()
		{
			SECRET_KEY = Convert.ToByte(random.Next(0, 255));// Convert.ToInt32((Time.time * 1000));
		}

		public static EncryptFloat Get(float v)
		{
			EncryptFloat ef = v;
			return ef;
		}

		public static implicit operator EncryptFloat(float value)
		{
			EncryptFloat encryptFloat = new EncryptFloat();
			encryptFloat.Value = value;

			return encryptFloat;
		}

		public static implicit operator float(EncryptFloat encryptFloat)
		{
			return encryptFloat.Value;
		}

		public string ToString(string format)
		{
			return Value.ToString(format);
		}

		public override string ToString()
		{
			return Value.ToString();
		}
	}
}